package com.wxm.log.api.event;

import com.wxm.log.api.entity.OpLogEntity;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.function.Consumer;

/**
 * <p>
 * 异步监听日志事件
 * </p>
 *
 * @author: wxm
 * @Date: 2021/10/5
 */
public class LogListener {
    private Consumer<OpLogEntity> consumer;
    public LogListener(Consumer<OpLogEntity> consumer) {
        this.consumer = consumer;
    }

    @Async
    @Order
    @EventListener(LogEvent.class)//监听LogEvent的事件
    public void saveLog(LogEvent event){
        OpLogEntity log = (OpLogEntity)event.getSource();
        //将日志信息传递给consumer
        consumer.accept(log);
    }
}
